package org.munoz.shared;

import java.util.ArrayList;

public class Player {
	private Deck hand;
	private int id;
	private ArrayList<Rank> playerFourRankCardsPlacedInTable = new ArrayList<Rank>();
	
	public Player(int id){
		hand = new Deck();
		this.id = id;
	}
	
	public void receiveCard(Card c){
		hand.addOneCard(c);
	}
	
	public Player getPlayerCopy() {
	    Player p = new Player(id);
	    p.hand = hand.getDeckCopy();
	    p.playerFourRankCardsPlacedInTable = new ArrayList<Rank>();
	    for(Rank r : playerFourRankCardsPlacedInTable){
	    	p.playerFourRankCardsPlacedInTable.add(r);
	    }
	    return p;
	}
	
	public int getNumberOfCardsInHand(){
		return hand.getNumberOfCardsRemaining();
	}
	
	public int getNumberOfRankGroups(){
		return playerFourRankCardsPlacedInTable.size();
	}
	
	public Deck getHand(){
		return hand;
	}
	
	public ArrayList<Rank> getPlayerFourRankCardsPlacedInTable() {
		return playerFourRankCardsPlacedInTable;
	}

	public void setPlayerFourRankCardsPlacedInTable(
			ArrayList<Rank> playerFourRankCardsPlacedInTable) {
		this.playerFourRankCardsPlacedInTable = playerFourRankCardsPlacedInTable;
	}
	
	public boolean[] check4CardSameRankInHand(){
		int ranks[] = new int[13];
		for(Card c : hand.getCards()){
			ranks[c.getRank().ordinal()]++;
		}
		boolean [] placeInTable = new boolean[13];
		for(int i=0; i<13; i++){
			if(ranks[i]==4){
				placeInTable[i] = true;
			}
		}
		return placeInTable;
	}
	public void add4CardSameRankToTable(Rank r){
		playerFourRankCardsPlacedInTable.add(r);
		for(int i=hand.getNumberOfCardsRemaining(); i>0; i--){
			if(hand.getCards().get(i-1).getRank() == r){
				hand.removeCardDeck(i-1);
			}
		}
	}
	public int getId(){
		return id;
	}
}